/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.recycling.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.recycling.api.entity.RecyclingSetting;
import com.medusa.gruul.recycling.mapper.RecyclingSettingMapper;
import com.medusa.gruul.recycling.model.dto.BtnStyleDto;
import com.medusa.gruul.recycling.model.dto.CouponModifyDto;
import com.medusa.gruul.recycling.model.dto.FundModifyDto;
import com.medusa.gruul.recycling.model.dto.PopupWindowDto;
import com.medusa.gruul.recycling.model.vo.RecyclingSettingVo;
import com.medusa.gruul.recycling.service.IRecyclingSettingService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 回收设置表 服务实现类
 * </p>
 *
 * @author whh
 * @since 2021-04-28
 */
@Service
public class RecyclingSettingServiceImpl extends ServiceImpl<RecyclingSettingMapper, RecyclingSetting> implements IRecyclingSettingService {

    @Override
    public RecyclingSettingVo getByTenantIdSetting() {
        RecyclingSetting recyclingSetting = this.baseMapper.selectOne(
                new QueryWrapper<RecyclingSetting>()
                        .eq("shop_id", ShopContextHolder.getShopId())
        );
        if (recyclingSetting == null) {
            //不存在则初始化店铺相关数据
            recyclingSetting = new RecyclingSetting();
            recyclingSetting.setId(0L);
            recyclingSetting.setShopId(ShopContextHolder.getShopId());
            recyclingSetting.setGoodsFundState(false);
            recyclingSetting.setFundQuota(new BigDecimal("0"));
            recyclingSetting.setPageBgImg("");
            recyclingSetting.setBuyBgImg("");
            recyclingSetting.setHomePromptBgImg("");
            recyclingSetting.setRecyclingState(false);
            recyclingSetting.setEntranceState(false);
            List<BtnStyleDto> btns = CollectionUtil.newArrayList(
                    new BtnStyleDto("旧衣回收","#409EFF",1),
                    new BtnStyleDto("加入购物车","#869926",2),
                    new BtnStyleDto("立即购买","#409EFF",3)
            );
            recyclingSetting.setBtnStyleJson(JSON.toJSONString(btns));
            recyclingSetting.setAssCouponId(0L);
            recyclingSetting.setPageCarousels("");
            recyclingSetting.setActivityDesc("");
            recyclingSetting.setSubmitImg("");
            recyclingSetting.setTenantId(TenantContextHolder.getTenantId());
            this.baseMapper.insert(recyclingSetting);
        }
        RecyclingSettingVo recyclingSettingVo = BeanUtil.toBean(recyclingSetting, RecyclingSettingVo.class);
        if (StrUtil.isNotEmpty(recyclingSetting.getBtnStyleJson())) {
            recyclingSettingVo.setBtnStyles(JSON.parseArray(recyclingSetting.getBtnStyleJson()).toJavaList(BtnStyleDto.class));
        }
        return recyclingSettingVo;
    }


    /**
     * 获取当前租户回收相关信息
     *
     * @param id 表id
     * @return com.medusa.gruul.recycling.api.entity.RecyclingSetting
     */
    private RecyclingSetting isCurrentTenantId(Long id) {
        RecyclingSetting recyclingSetting = this.getById(id);
        if (recyclingSetting == null) {
            throw new ServiceException("无效数据");
        }
        if (!recyclingSetting.getTenantId().equals(TenantContextHolder.getTenantId())) {
            throw new ServiceException("非有效商户数据");
        }
        return recyclingSetting;
    }


    @Override
    public void fundModify(FundModifyDto settingDto) {
        RecyclingSetting recyclingSetting = isCurrentTenantId(settingDto.getId());
        RecyclingSetting rs = new RecyclingSetting();
        rs.setId(recyclingSetting.getId());
        rs.setGoodsFundState(settingDto.getGoodsFundState());
        rs.setFundQuota(settingDto.getFundQuota());
        rs.setPageBgImg(settingDto.getPageBgImg());
        rs.setBuyBgImg(settingDto.getBuyBgImg());
        this.baseMapper.updateById(rs);
    }

    @Override
    public void couponModify(CouponModifyDto couponModifyDto) {
        if (couponModifyDto.getRecyclingState()){
            if (couponModifyDto.getAssCouponId() == null || couponModifyDto.getAssCouponId() < 0
                    || couponModifyDto.getPageCarousels().isEmpty() || couponModifyDto.getActivityDesc().isEmpty()
                    || couponModifyDto.getSubmitImg().isEmpty()){
                throw new ServiceException("信息不能为空");
            }
            if(couponModifyDto.getEntranceState()){
                if (ArrayUtil.isEmpty(couponModifyDto.getBtnStyles())){
                    throw new ServiceException("页面轮播图不能为空");
                }
            }
        }else {
            if (couponModifyDto.getEntranceState()){
                throw new ServiceException("商品详情回收入口不能开启");
            }
        }
        RecyclingSetting recyclingSetting = isCurrentTenantId(couponModifyDto.getId());

        RecyclingSetting rs = new RecyclingSetting();
        rs.setId(recyclingSetting.getId());
        rs.setRecyclingState(couponModifyDto.getRecyclingState());
        rs.setEntranceState(couponModifyDto.getEntranceState());
        rs.setBtnStyleJson(JSON.toJSONString(couponModifyDto.getBtnStyles()));
        rs.setAssCouponId(couponModifyDto.getAssCouponId());
        rs.setPageCarousels(couponModifyDto.getPageCarousels());
        rs.setActivityDesc(couponModifyDto.getActivityDesc());
        rs.setSubmitImg(couponModifyDto.getSubmitImg());
        this.baseMapper.updateById(rs);
    }


    @Override
    public void popupWindow(PopupWindowDto popupWindowDto) {
        RecyclingSetting recyclingSetting = isCurrentTenantId(popupWindowDto.getId());
        RecyclingSetting rs = new RecyclingSetting();
        rs.setId(recyclingSetting.getId());
        rs.setHomePromptBgImg(popupWindowDto.getHomePromptBgImg());
        this.baseMapper.updateById(rs);
    }
}
